import { getToken, setToken, removeToken } from '@/utils/auth'
import { loginRequest, getUserInfo } from '@/api/user'
import { constantRoutes,resetRouter } from "@/router";
export default {
  state () {
    return {
      token: getToken(),
      userInfo: {},
      routes:[]
    }
  },
  mutations: {
    setToken (state, token) {
      state.token = token
      setToken(token)
    },
    //移除token
    removeTokenMutations (state) {
      //清空token,是恢复到初始值, undefined
      state.token = undefined
      removeToken()
    },
    //设置用户信息
    setUserInfo (state, payload) {
      state.userInfo = payload
    },
    //清空用户信息
    removeUserInfo (state) {
      state.userInfo = {}
    },
    setRoutesMutations(state, newRoutes) {
      state.routes = [...constantRoutes, ...newRoutes]
    }
    
  },
  actions: {

    async setTokenActions ({ commit }, from) {

      try {
       const res = await loginRequest(from)
        commit('setToken', res.data)
      } catch (error) {
        console.log(error)

      }

    },
    //获取用户头像等信息
    async setUserInfoActions ({ commit }) {
      try {
        const res = await getUserInfo()
        console.log(res,1111);
        commit('setUserInfo', res.data)
        return res.data
      } catch (error) {
        console.log(error)

      }
    },
    //退出登录
    logout ({ commit }) {
      //清空token
      commit('removeTokenMutations')
      //清空用户信息
      commit('removeUserInfo')
      //清空重复提醒路由
      resetRouter()
    },
   
  },
  getters: {},
  namespaced: true
}